package com.lingz.manager.modules.camera.serviceimpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingz.manager.common.vo.PageVo;
import com.lingz.manager.modules.camera.dao.mapper.EquipmentMapper;
import com.lingz.manager.modules.camera.entity.Equipment;
import com.lingz.manager.modules.camera.service.EquipmentService;
import com.lingz.manager.modules.camera.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 相机设备列表接口实现
 *
 * @author hanyuhang
 */
@Slf4j
@Service
@Transactional
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private CommonUtils commonUtils;

    @Override
    public List<Equipment> findListEquipment(Equipment entity) {
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        List<Equipment> list = equipmentMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Page<Equipment> findPageEquipment(Equipment entity, PageVo pageVo) {
        // 当前页数
        int pageNumber = pageVo.getPageNumber();
        // 每页显示的条数
        int pageSize = pageVo.getPageSize();

        Page<Equipment> page = new Page<>(pageNumber, pageSize);

        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        Page<Equipment> pageList = equipmentMapper.selectPage(page, wrapper);
        return pageList;
    }

    @Override
    public String addEquipment(Equipment entity) {
        equipmentMapper.insert(entity);
        return "操作成功";
    }

    @Override
    public String editEquipment(Equipment entity) {
        equipmentMapper.updateById(entity);
        return "操作成功";
    }

    @Override
    public String deleteEquipment(String[] ids) {
        equipmentMapper.deleteBatchIds(Arrays.asList(ids.clone()));
        return "操作成功";
    }

    @Override
    public Map<String, Object> login(String json) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 解析json
            Equipment equipment = new Equipment();
            Map<String, Object> map = JSONObject.parseObject(json, Map.class);
            equipment.setEquSerial(map.get("serial").toString());
            equipment.setEquEncoderContent(map.get("encoderContent").toString());
            equipment.setType(map.get("type").toString());

            if (map.containsKey("cameras")) {
                List<Map> cameras = JSONArray.parseArray(map.get("cameras").toString(), Map.class);
                if (null != cameras && cameras.size() > 0) {
                    if (cameras.get(0).containsKey("serial")) {
                        equipment.setCamSerial(cameras.get(0).get("serial").toString());
                    }
                    if (cameras.get(0).containsKey("name")) {
                        equipment.setName(cameras.get(0).get("name").toString());
                    }
                    if (cameras.get(0).containsKey("encoderContent")) {
                        equipment.setCamEncoderContent(cameras.get(0).get("encoderContent").toString());
                    }
                }
            }
            // 判断当前这个设备是否存在
            QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
            wrapper.eq("equ_serial", equipment.getEquSerial());
            // 调用查询接口
            Equipment one = equipmentMapper.selectOne(wrapper);
            if (null != one) {
                equipmentMapper.update(equipment, wrapper);
            } else {
                // 保存数据
                equipmentMapper.insert(equipment);
            }
            result.put("code", "200");
            result.put("message", "success");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "500");
            result.put("message", "fail");
        }
        return result;
    }

    @Override
    public Map<String, Object> heartBeat(String json) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Map<String, Object> map = JSONObject.parseObject(json, Map.class);
            // 设备序列号
            String serial = map.get("serial").toString();
            // 设备IP
            String ip = map.get("ip").toString();

            QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
            wrapper.eq("equ_serial", serial);
            Equipment equipment = equipmentMapper.selectOne(wrapper);
            if (null != equipment) {
                equipment.setIp(ip);
                equipmentMapper.update(equipment, wrapper);
                result.put("code", "200");
                result.put("message", "success");
                data.put("msg", "logined");
                data.put("ver", "1.0.0");
                data.put("stime", sdf.format(new Date()));
                data.put("level", "1");
                result.put("data", data);
            } else {
                result.put("code", "200");
                result.put("message", "success");
                data.put("msg", "notlogin");
                data.put("ver", "1.0.0");
                data.put("stime", sdf.format(new Date()));
                data.put("level", "1");
                result.put("data", data);
            }
        } catch (Exception e) {
            result.put("code", "500");
            result.put("message", "success");
            data.put("msg", "error");
            data.put("ver", "1.0.0");
            data.put("stime", sdf.format(new Date()));
            result.put("data", data);
            data.put("level", "1");
            e.printStackTrace();
        }
        return result;
    }
}